Go Details & Tips 101 (20240316) by Tapir Liu
Author:Tapir Liu
Language: eng
Format: epub
The famous := trap
Let's view a simple program.
package main import "fmt" import "strconv" func parseInt(s string) (int, error) { n, err := strconv.Atoi(s) if err != nil { fmt.Println("err:", err) b, err := strconv.ParseBool(s) if err != nil { return 0, err } fmt.Println("err:", err) if b { n = 123 } } return n, err } func main() { fmt.Println(parseInt("true")) }
We know that the call strconv.Atoi(s) will return a non-nil error, but the call strconv.ParseBool(s) will return a nil error. Then, will the call parseInt("true") return a nil error, too? The answer is it will return a non-nil error. The outputs of the program is shown below:
err: strconv.Atoi: parsing "true": invalid syntax err: <nil> 123 strconv.Atoi: parsing "true": invalid syntax
Wait, isn't the err variable is re-declared in the inner code block and its value has been modified to nil before the parseInt("true") returns? This is a confusion many new Go programmers, including me, ever encountered when they just started using Go.
The reason why the call parseInt("true") returns a non-nil error is a variable declared in an inner code block is never a re-declaration of a variable declared in an outer code block. Here, the inner declared err variable is set (initialized) as nil. It is not a re-declaration (a.k.a. modification) of the outer declared err variable. The outer one is set (initialized) as a non-nil value, then it is never changed later.
There is the voice to remove the ... := ... re-declaration syntax form from Go. But it looks this is a too big change for Go. Personally, I think explicitly marking the re-declared variables out is a more feasible solution.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Understanding PDA Autism in Kids: A Guide for Parents and Teachers to Support Neurodiverse Learners by Jehu Len(525)
Chicken Soup for the Soul Presents Teens Talkin' Faith by Jack Canfield(472)
Brain Teasers to Build Critical Thinking Skills by Safarova Kris(410)
Brain Teasers to Build Critical Thinking Skills: Brain Exercises for Tech, Banking, Case Interview Prep, and to Keep Your Mind Sharp by Kris Safarova(399)
The Victorian Era: A Captivating Guide to the Life of Queen Victoria and an Era in the History of the United Kingdom Known for Its Hierarchy-Based Social Order by Captivating History(392)
100 Ideas for Secondary Teachers: Engaging Parents by Janet Goodall & Kathryn Weston(381)
Python 101 - Fundamentals by Sam(362)
Critical Curriculum Leadership : A Framework for Progressive Education by Rose M. Ylimaki(351)
Writing Solid Code: Development Philosophies for Writing Bug-Free Programs by Steve Maguire(349)
The Art of Emotional Validation: Improve Your Communication Skills and Transform Your Relationships by Validating Emotions and Feelings by Emily Wright(331)
Intersectionality in Educational Research by Dannielle Joy Davis; James L. Olive; Rachelle J. Brunn-Bevel; Susan R. Jones(322)
A Beginner's Guide to SSD Firmware by Unknown(320)
The Knights Templar: An Enthralling History of the Rise and Fall of the Most Influential Catholic Military Order by Wellman Billy(316)
The Future Knowledge Compendium by Ellyard Peter;(306)
How to be assertive in any situation by Hadfield Sue & Hasson Gill(306)
Foundations of Educational Research by Victoria Elliott(301)
Making Connections in and Through Arts-Based Educational Research by Hala Mreiwed Mindy R. Carter Sara Hashem Candace H. Blake-Amarante(298)
Message from the Pleiades; The Contact Notes of Eduard Billy Meier v1 only by unknow(295)
What Every Teacher Should Know about Learning, Memory, and the Brain by Tileston Donna E. Walker;(295)
